<template>
  <div class="article-comments">
    <!-- 评论列表 -->
    <van-list
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
    >
      <van-cell
        v-for="(item, idx) in list"
        :key="idx"
      >
        <van-image
          slot="icon"
          round
          width="30"
          height="30"
          style="margin-right: 10px;"
          src="item.aut_ptoto"
        />
        <span style="color: #466b9d;" slot="title">{{item.aut_name}}</span>
        <div slot="label">
          <p style="color: #363636;">{{item.content}}</p>
          <p>
            <span style="margin-right: 10px;">{{item.pubdate | dateFormat}}</span>
            <van-button
            size="mini"
            type="default"
            @click="toShowReply(item)"
            >回复{{item.reply_count}}</van-button>
          </p>
        </div>
        <van-icon slot="right-icon" :name="item.is_liking ? 'like' : 'like-o'" @click="toSelect(item)" />
      </van-cell>
      <van-popup
      v-model="show"
      position="bottom"
      :style="{ height: '80%' }" >
      <!-- 评论回复组件 -->
          <comment-reply
          :comment='currentComment'
          :articleId='articleId'
          v-if="show"
          @close="show=false"
          ></comment-reply>
           <!-- 评论回复组件 -->
      </van-popup>
    </van-list>
    <!-- 评论列表 -->

    <!-- 发布评论 -->
    <van-cell-group class="publish-wrap">
      <van-field
        clearable
        placeholder="请输入评论内容"
        v-model.trim="content"
      >
        <van-button slot="button" size="mini" type="info" @click="PaddComment">发布</van-button>
      </van-field>
    </van-cell-group>
    <!-- /发布评论 -->
  </div>
</template>

<script>
import commentReply from './commentReply'
import { addComment, getComments, addCommentLike, cancelCommentLike } from '@/api/comment'
export default {
  name: 'ArticleComment',
  components: {
    commentReply
  },
  props: {
    articleId: {
      type: String,
      required: true
    }
  },
  data () {
    return {
      // 当前评论回复组件会的评论是哪一条
      currentComment: {},
      show: false,
      content: '',
      offset: null,
      list: [], // 评论列表
      loading: false, // 上拉加载更多的 loading
      finished: false // 是否加载结束
    }
  },

  methods: {
    // 显示回复区域的内容 将点击的那个部分的内容进行传值
    toShowReply (item) {
      this.show = true
      this.currentComment = item
    },
    // 获取评论
    async onLoad () {
      const res = await getComments(this.articleId, this.offset)
      const results = res.data.data.results
      // 加载状态结束
      this.list.push(...results)
      this.loading = false
      // 数据全部加载完成
      if (results.length === 0) {
        this.finished = true
      }
      this.offset = res.data.data.last_id
    },
    // 添加评论功能
    async PaddComment () {
      if (this.comment === '') {
        return
      }
      const res = await addComment(this.articleId, this.content)
      this.list.unshift(res.data.data.new_obj)
      this.content = ''
      this.$toast.success('发表成功')
    },
    // 对评论是否点赞
    async toSelect (item) {
      try {
        const commentId = item.com_id.toString()
        if (item.is_liking) {
          await cancelCommentLike(commentId)
        } else {
          await addCommentLike(commentId)
        }
        item.is_liking = !item.is_liking
        if (item.is_liking) {
          this.$toast.success('点赞okk')
        } else {
          this.$toast.success('取消点赞')
        }
      } catch (error) {
        this.$toast.fail('操作失败')
      }
    }
  }
}
</script>

<style scoped lang='less'>
// 发表评论的区域是固定在下端的
.publish-wrap {
  position: fixed;
  left: 0;
  bottom: 0;
  width: 100%;
}
// 给发表评论区空出地方
.van-list {
  margin-bottom: 45px;
}
</style>
